package com.collect.tanzania.zhiScene.zhiWidget.zhiDateDialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.collect.tanzania.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ProductionCalendarDialogFragment extends DialogFragment implements OnDateSelectedListener {
    private TextView tv_year;
    private TextView tv_monthAndDay;
    private TextView tv_weekDay;

    private OnDateSelectedItemClickListener onItemClickListener;

    private Calendar calendar;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private String mDateString;
    //日期类型：1制订计划，2流水
    private String type;

    public ProductionCalendarDialogFragment() {
        this.type = "1";
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setOnDateSelectedItemClickListener(OnDateSelectedItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_basic, null);
        MaterialCalendarView calendarView = (MaterialCalendarView) view.findViewById(R.id.calendarView);

        tv_year = (TextView) view.findViewById(R.id.tv_year);
        tv_monthAndDay = (TextView) view.findViewById(R.id.tv_monthAndDay);
        tv_weekDay = (TextView) view.findViewById(R.id.tv_weekDay);
        tv_year.setText(calendar.get(Calendar.YEAR) + "-");
        tv_monthAndDay.setText(calendar.get(Calendar.MONTH) + 1 + "-" + calendar.get(Calendar.DAY_OF_MONTH));
        tv_weekDay.setText(numToChinese(calendar.get(Calendar.DAY_OF_WEEK)));

        int gmonth = (calendar.get(Calendar.MONTH) + 1);
        int gDay = calendar.get(Calendar.DAY_OF_MONTH);
        String gmonthStr = gmonth < 10 ? "0" + gmonth : gmonth + "";
        String gDayStr = gDay < 10 ? "0" + gDay : gDay + "";

        mDateString = (calendar.get(Calendar.YEAR) + "-" + gmonthStr + "-" + gDayStr);

        try {
            calendarView.setOnDateChangedListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendarView.setSelectedDate(calendar);
        calendarView.addDecorator(new PrimeDayAbleDecorator());

        return new AlertDialog.Builder(getActivity(), R.style.ProDateDialog)
                .setView(view)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        onItemClickListener.onItemClick(mDateString);
                    }
                }).create();
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView materialCalendarView, @NonNull CalendarDay date, boolean selected) {

        String tag = getTag();
        String day = sdf.format(date.getDate());
        mDateString = day;
        tv_year.setText(date.getYear() + "-");
        tv_monthAndDay.setText((date.getMonth() + 1) + "-" + date.getDay());
        tv_weekDay.setText(numToChinese(date.getCalendar().get(Calendar.DAY_OF_WEEK)));
    }

    private String numToChinese(int num) {
        String s = "";
        switch (num) {
            case 1:
                s = getResources().getString(R.string.date_seven);
                break;
            case 2:
                s = getResources().getString(R.string.date_one);
                break;
            case 3:
                s = getResources().getString(R.string.date_two);
                break;
            case 4:
                s = getResources().getString(R.string.date_three);
                break;
            case 5:
                s = getResources().getString(R.string.date_four);
                break;
            case 6:
                s = getResources().getString(R.string.date_five);
                break;
            case 7:
                s = getResources().getString(R.string.date_six);
                break;
        }
        return s;

    }

    private static class PrimeDayDisableDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return day.getCalendar().compareTo(Calendar.getInstance()) == -1;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(true);
        }
    }


    private static class PrimeDayAbleDecorator implements DayViewDecorator {

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return false;//day.getCalendar().compareTo(Calendar.getInstance()) == -1;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setDaysDisabled(false);
        }


    }
}
